package cn.aran.product.service.impl;

import cn.aran.basic.exception.BusinessException;
import cn.aran.basic.service.impl.BaseServiceImpl;
import cn.aran.basic.utils.BaiduAuditUtils;
import cn.aran.product.domain.Product;
import cn.aran.product.domain.ProductDetail;
import cn.aran.product.domain.ProductOnlineAuditLog;
import cn.aran.product.mapper.ProductDetailMapper;
import cn.aran.product.mapper.ProductMapper;
import cn.aran.product.mapper.ProductOnlineAuditLogMapper;
import cn.aran.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author aran
 * @since 2022-07-21
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;

    /** 
    * @Title: save
    * @Description: 保存
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/21 10:13  
    * @Parameters: [product]
    * @Return void
    */
    @Override
    public void save(Product product) {
        productMapper.save(product);
        ProductDetail detail = product.getDetail();
        detail.setProductId(product.getId());
        productDetailMapper.save(detail);
    }

    /** 
    * @Title: update
    * @Description: 修改
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/21 10:13  
    * @Parameters: [product]
    * @Return void
    */
    @Override
    public void update(Product product) {
        productMapper.update(product);
        productDetailMapper.update(product.getDetail());
    }
    
    /** 
    * @Title: remove
    * @Description: 删除
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/21 10:14  
    * @Parameters: [id]
    * @Return void
    */
    @Override
    public void remove(Long id) {
        productMapper.remove(id);
        productDetailMapper.removeByProductId(id);
    }

    /**
    * @Title: batchRemove
    * @Description: 批量删除
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/21 14:18
    * @Parameters: [ids]
    * @Return void
    */
    @Override
    public void batchRemove(List<Long> ids) {
        productMapper.batchRemove(ids);
        productDetailMapper.batchRemoveByProductIds(ids);
    }

    /**
    * @Title: onsale
    * @Description: 批量上架
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/21 10:16  
    * @Parameters: [ids]
    * @Return void
    */
    @Override
    public void onsale(List<Long> ids) {
        List<Long> listIds = new ArrayList<>();
        for (Long id : ids) {
            Product product = productMapper.loadById(id);
            StringBuffer sb = new StringBuffer();
            // 2.调用百度AI进行文字审核,图片审核
            if (!BaiduAuditUtils.TextCensor(product.getName())) {
                sb.append("服务名称不合法，服务名称为：" + product.getName());
            }else {
                listIds.add(id);
            }
            if (sb.length() > 0) {
                // 违规
                ProductOnlineAuditLog productOnlineAuditLog = new ProductOnlineAuditLog();
                productOnlineAuditLog.setProductId(id);
                productOnlineAuditLog.setState(0);
                productOnlineAuditLog.setNote(sb.toString());
                productOnlineAuditLogMapper.save(productOnlineAuditLog);
                continue;
            }
        }
        productMapper.onsale(listIds);
    }

    /** 
    * @Title: offsale
    * @Description: 批量下架
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/21 10:17  
    * @Parameters: [ids]
    * @Return void
    */
    @Override
    public void offsale(List<Long> ids) {
        productMapper.offsale(ids);
    }

    /**
    * @Title: getDetail
    * @Description: 服务详情
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/21 10:18
    * @Parameters: [id]
    * @Return cn.aran.product.domain.ProductDetail
    */
    @Override
    public ProductDetail getDetail(Long id) {
        return productDetailMapper.loadByProductId(id);
    }
}
